﻿/// -----------------------------------------------------------------------
/// (c) 2009 Microsoft Corporation -- All rights reserved
/// This code is licensed under the MS-PL
/// http://www.opensource.org/licenses/ms-pl.html
/// Courtesy of the Open Source Techology Center: http://port25.technet.com
/// -----------------------------------------------------------------------

using System;
using System.IO;
using gsToolkit;
using System.Linq; // for .Last()
 
namespace CoApp {
    class Program {
 static string help = @"
About:
------
CoApp is a command-line tool to assist developing software for the 
Common Opensource Application Packaging Platform (CoApp).

Usage:
-------

CoApp [options]

    options:
    --------
    --help                      this help
    --nologo                    don't display the logo
    --load-config=<configfile>  loads options from configuration file
                                List the options, one per line, without the
                                prefix '--'.

    --update                    updates the existing configuration file with
                                changed version numbers

    --save                      updates the existing configuration file with 
                                changed version numbers, and adds any other 
                                command line options passed


    --major=<value>             Sets the major version number of the project
    --minor=<value>             Sets the minor version number of the project
    --revision=<value>          Sets the revision number of the project
    --build=<value>             Sets the build number of the project

    --increment-build           Increments the build number of the project and 
                                updates the config file
    --increment-revision        Increments the revision number of the project
                                and updates the config file
                


Notes:
------

";

      private static bool failTripped = false;

        static int Main(string[] args) {
            return new Program().main(args);
        }

        int main(string[] args) {
            CoAppTool coapptool = new CoAppTool();

            #region Options and Parameters)
            // config settings pass one, load the project file.
            foreach(var arg in args.Switches().Keys) {
                var parameters = args.Switches()[arg];

                switch (arg) {
                        /* global switches */
                    case "load-config":
                        // The config file is loaded, but if it needs to be saved 
                        // back, we'll have to write to this file.
                        coapptool.ProjectFilename = parameters.Last();
                        break;

                    case "nologo":
                        coapptool.Assembly().SetLogo("");
                        break;

                    case "help":
                        Logo();
                        return Help();

                    case "project":
                        coapptool.ProjectFilename = parameters.Last();
                        break;
                }
            }

            int i;
            foreach(var arg in args.Switches().Keys) {
                var parameters = args.Switches()[arg];

                switch(arg) {
                    case "major":
                        if(!Int32.TryParse(parameters.Last(), out i))
                            Fail("Invalid Switch 'major'. Value must be an integer.");
                        coapptool.Project.Major = i;
                        break;

                    case "minor":
                        if(!Int32.TryParse(parameters.Last(), out i))
                            Fail("Invalid Switch 'minor'. Value must be an integer.");
                        coapptool.Project.Minor = i;
                        break;

                    case "revision":
                        if(!Int32.TryParse(parameters.Last(), out i))
                            Fail("Invalid Switch 'revision'. Value must be an integer.");
                        coapptool.Project.Revision = i;
                        break;

                    case "build":
                        if(!Int32.TryParse(parameters.Last(), out i))
                            Fail("Invalid Switch 'build'. Value must be an integer.");
                        coapptool.Project.Build= i;
                        break;

                    case "increment-revision":
                        coapptool.Project.Revision++;
                        // implies --update
                        break;

                    case "increment-build":
                        coapptool.Project.Build++;
                        // implies --update
                        break;


                    default:
                        Logo();
                        return Fail("Error: unrecognized switch:{0}", arg);
                }
            }

            #endregion

            
            AspNetRuntime runtime = new AspNetRuntime("gws");
            runtime.Run(new[]{"test.aspx"});
            

            Logo();
            return coapptool.Run();
        }

        

        #region fail/help/logo
        public static int Fail(string text, params object[] par) {
            failTripped = true;
            using(new ConsoleColors(ConsoleColor.Red, ConsoleColor.Black))
                Console.WriteLine("Error:{0}", text.format(par));
            return 1;
        }

        int Help() {
            using(new ConsoleColors(ConsoleColor.White, ConsoleColor.Black))
                help.Print();
            return 0;
        }

        void Logo() {
            using(new ConsoleColors(ConsoleColor.Cyan, ConsoleColor.Black))
                this.Assembly().Logo().Print();
            this.Assembly().SetLogo("");
        }
        #endregion
    }
}
